/*****************************************************************************
* File Name: audio.h
*
* Version: 1.0
*
* Description:
* This file contains API for reading audio data through I2S interface
*
* Note:
* Defining DISABLE_AUDIO in platform.h to disable audio module
*
* Owner: MRAO
* 
* Related Document:
* I2S component datasheet
*
* Hardware Dependency:
* INMP441 Audio Codec
*
* Code Tested With:
* 1. PSoC Creator 3.1 
* 2. ARM GCC 4.8.4
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/
#ifndef _AUDIO_H_
#define _AUDIO_H_

#include "platform.h"
#include "device.h"


#define NUMBER_OF_BUFFERS               (60u)   /*Number of audio buffers used*/
#if 0
#define BUFFER_SIZE                     (20u)   /*Audio buffer size*/
#define INITIAL_BUFFER_DATA             (0u)    /* Initial data in the buffer */
#define NUMBER_OF_PACKET_TO_DROP        (320u)  /* Number of packet to be dropped before sending data over BLE */

#define AUDIO_SAMPLE_MIN                (-32768) /* Audio sample should be in 32676 to -32768 */
#define AUDIO_SAMPLE_MAX                (32767)  /* AUDIO_SAMPLE_MIN and AUDIO_SAMPLE_MAX represent those limits */
#define ADPCM_INDEX_MAX                 (88)     /* Maximum ADPCM index */ 
#define LOWER_NIBBLE_MASK               (0xF)    /* Lower nibble  mask*/
#define LOWER_BYTE_MASK                 (0xFF)   /* Lower byte mask */
#endif

#ifdef AUDIO_SEND_FREQUENT_SYNC_PACKET
#define AUDIO_SYNC_TIMESTAMP            (500)
#endif /* AUDIO_SEND_FREQUENT_SYNC_PACKET */

#ifdef	AUDIO_ADC_SAMPLE
#ifndef DISABLE_AUDIO_NOISE_FILTER
#define NOISE_FILTER_INTERVAL            (uint16)(100*1000/(1000/16)) //ms
#define NOISE_THRESHOLD                  25
#define NOISE_COUNT_THRESHOLD            6u
#define NOISE_FILTER_LIMT								 16u
#define NOISE_FILTER_LEN								 NOISE_FILTER_INTERVAL
#endif/*DISABLE_AUDIO_NOISE_FILTER*/ 

#define NUMBER_OF_PACKET_TO_DROP        (1000u)  /* Number of packet to be dropped before sending data over BLE */


#define ADC_Wakeup       								ADC_SAR_SEQ_Wakeup  
#define ADC_StartConvert  							ADC_SAR_SEQ_StartConvert  
#define ADC_IsEndConversion 						ADC_SAR_SEQ_IsEndConversion
#define ADC_GetResult16   							ADC_SAR_SEQ_GetResult16
#define ADC_StopConvert 								ADC_SAR_SEQ_StopConvert 
#define ADC_Sleep 											ADC_SAR_SEQ_Sleep 
#define ADC_Start 											ADC_SAR_SEQ_Start 
#define ADC_Stop  											ADC_SAR_SEQ_Stop  
#define ADC_Enable 											ADC_SAR_SEQ_Enable
#define ADC_IRQ_Stop 										ADC_SAR_SEQ_IRQ_Stop	

#endif/*AUDIO_ADC_SAMPLE*/

#ifndef DISABLE_AUDIO                   /*Macro to disable Audio module */

#ifndef AUDIO_ADC_SAMPLE
#define Audio_Codec_Enable()            Audio_Select_Write(1) /*Write GPIO 1 to enable Codec*/
#define Audio_Codec_Disable()           Audio_Select_Write(0) /*Write GPIO 0 to disable Codec*/
#else
//////////////////////////////////////////////////
/// \def adc channel
/// 
//////////////////////////////////////////////////
#define CH0_N               				(0x00u)
#endif/*AUDIO_ADC_SAMPLE*/


#define Audio_IsActive()                (isAudioActive)
extern bool isAudioActive;

/*****************************************************************************
* Function Name: Audio_Init()
******************************************************************************
* Summary:
* This functions is used to initialize the audio module
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Audio_Init(void);

/*****************************************************************************
* Function Name: Audio_Start()
******************************************************************************
* Summary:
* This functions is used to start reading the audio data from the codec 
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Audio_Start(void);

/*****************************************************************************
* Function Name: Audio_Poll()
******************************************************************************
* Summary:
* This functions is polls if any audio data is available
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Audio_Poll(void);

/*****************************************************************************
* Function Name: Audio_Get_Report()
******************************************************************************
* Summary:
* This functions is packetizes the audio data if available
*
* Parameters:
* report - Report structure will be filled if any audio event occurs
*
* Return:
* ReportType - AUDIO_REPORT If any audio packet is available else NO_REPORT
*****************************************************************************/
extern ReportType Audio_Get_Report(Report *report);

/*****************************************************************************
* Function Name: Audio_Stop()
******************************************************************************
* Summary:
* This functions is used to stop audio module
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Audio_Stop(void);

#else
#define AUDIO_ENABLE
#define AUDIO_DISABLE

#define Audio_Init()
#define Audio_Start()
#define Audio_Poll()
#define Audio_IsActive()                     (false)
#define Audio_Get_Report(report)             (NO_REPORT)
#define Audio_Stop()
#endif /* DISABLE_AUDIO */

#endif /* _AUDIO_H_ */
